package org.example.leetcode;

import org.example.two.datatype.MaxPQ;

import java.io.PushbackInputStream;

/**
 * @outher 刘旭
 * @date 2024/6/21 16:12
 */
public class Topic414 {

    public static void main(String[] args) {
        int[] acc = {3,3,4,3,4,3,0,3,3};
        int i = thirdMax(acc);
        System.out.print(i);
    }
    public static int thirdMax(int[] nums) {
        int N= nums.length;
        for (int i = N/2; i >= 0; i--) {
            sink(nums,i,N);
        }
        int max = nums[0];
        int n = 0;
        int k = max;
        for (int i = N-1; i >=0 ; i--) {
            if (k!=nums[0]){
                k=nums[0];
                n++;
                if (n==2){
                    return k;
                }
            }
            nums[0]=nums[i];
            sink(nums,0,i);
        }
        return max;
    }


    public static void sink(int[] nums,int k , int N){
        while (2*k+1<N){
            int j = 2*k+1;
            if (j+1<N && nums[j]<nums[j+1]){
                j++;
            }
            if (nums[k]>nums[j]){
                break;
            }
            int m =nums[k];
            nums[k]=nums[j];
            nums[j] = m;
            k=j;
        }

    }

}
